//
//  DeviceOptionMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "DeviceOptionMessage.h"

#define PATH                      @"device_options"

#define KEY_LIMIT @"limit"

@implementation DeviceOptionMessage

- (NSString *)getPath {
    return PATH;
}

- (BOOL) methodCallConsumed:(NSString*)method params:(NSDictionary*)params result:(FlutterResult)result
{
    BOOL consumed = YES;
    if ([super methodCallConsumed:method params:params result:result])
        return YES;
    
    assert(method != nil);
    NSNumber *enable = nil;
    NSNumber *number = nil;
    
    __weak typeof(self)weakSelf = self;
    
    if ([method isEqualToString:M_setContinuousHeartRate]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setContinueMeasureHeartRateSwitch:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setAutoHeartRate]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setAutoHeartRate:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setLowestRemind]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            number = [params objectForKey:KEY_LIMIT];
            [[UTEDeviceMgr sharedInstance] setLowestRemind:[enable boolValue] limit:[number integerValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setHighestRemind]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            number = [params objectForKey:KEY_LIMIT];
            [[UTEDeviceMgr sharedInstance] setHeartrateRaiseRemindSwitch: [enable boolValue] limit:[number integerValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setHeartRateZone]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_setContinuousBloodOxygen]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setPeriodSpo2Enable:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setSciSleep]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setProfessionalSleep:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setFutureWeatherCapability]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_getFutureWeatherSupport]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_setWeatherErrorInfo]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_setBloodOxygenRemind]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            number = [params objectForKey:KEY_LIMIT];
            [[UTEDeviceMgr sharedInstance] setPeriodSpo2Enable:[enable boolValue]  remindLimit: [number integerValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setTimeSyncEnable]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil) {
            [[SPHelper shareInstance] putObject:M_setTimeSyncEnable value:enable];
            result(@YES);
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setWeightSyncEnable]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil) {
            [[SPHelper shareInstance] putObject:M_setWeightSyncEnable value:enable];
            result(@YES);
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setScreenAutoLight]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setScreenAutoLight: [enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_getScreenAutoLightState]) {
        if (![self isConnected]) {
            result([NSDictionary dictionary]);
            return YES;
        }
        
        [[UTEDeviceMgr sharedInstance] getScreenAutoLightState:^(NSInteger switchType, NSInteger switchState, NSInteger errorCode, NSDictionary *uteDict) {
            if (errorCode == UTEDeviceErrorNil) {
                result([NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInteger:switchType], @"switchType",
                        [NSNumber numberWithInteger:switchState], @"switchState",
                        nil]);
            }
            else {
                result([NSDictionary dictionary]);
            }
        }];
    }
    else if ([method isEqualToString:M_setScreenOnDuration]) {
        number = [params objectForKey:KEY_VALUE];
        if (number != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setLightTime:[number integerValue] Block:^(NSInteger errorCode) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        result(@YES);
    }
    else if ([method isEqualToString:M_setFindWearCmd]) {
        number = [params objectForKey:KEY_VALUE];
        if (number != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setFindWearCmd: [number integerValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_getFindWearState]) {
        if (![self isConnected]) {
            result([NSDictionary dictionary]);
            return YES;
        }
        [[UTEDeviceMgr sharedInstance] getFindWearState:^(NSInteger findWearState, NSInteger errorCode, NSDictionary *uteDict) {
            if (errorCode == UTEDeviceErrorNil) {
                result([NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInteger:findWearState], @"findWearState",
                        nil]);
            }
            else {
                result([NSDictionary dictionary]);
            }
        }];
    }
    else if ([method isEqualToString:M_setDoNotDisturb]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_queryDeviceDoNotDisturb]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_setAlarmList]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSArray *alarmList = [params objectForKey:TYPE_LIST];
        NSMutableArray *alarmArray = [NSMutableArray array];
        for (NSDictionary *item in alarmList) {
            NSNumber *hour = [item objectForKey:@"hour"];
            NSNumber *min = [item objectForKey:@"minute"];
            NSString *name = [item objectForKey:@"name"];
            NSNumber *enable = [item objectForKey:@"enable"];
            NSNumber *index = [item objectForKey:@"index"];
            NSArray *cycle = [item objectForKey:@"cycleList"];

            UTEModelClock *clock = [[UTEModelClock alloc] init];
            clock.timeHour = [hour intValue];
            clock.timeMin = [min intValue];
            clock.name = name;
            clock.index = [index intValue];
            clock.cycle = [cycle.firstObject intValue];
            clock.enable = [enable boolValue]==1;
            [alarmArray addObject:clock];
        }
        [[UTEDeviceMgr sharedInstance] setAlarmArrayModel:alarmArray block:^(NSInteger errorCode,NSDictionary *uteDict) {
            result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
        }];
    }
    else if ([method isEqualToString:M_getAlarmList]) {
        if (![self isConnected]) {
            result([NSArray array]);
            return YES;
        }
        [[UTEDeviceMgr sharedInstance] getAlarmArray:^(NSInteger errorCode, NSDictionary *uteDict) {
            if (errorCode == UTEDeviceErrorNil) {
                NSArray* alarmArray = [[uteDict objectForKey:KEY_DATA] objectForKey:@"alarmArray"];
                NSMutableArray *newAlarmArr = [NSMutableArray array];
                for (NSDictionary *item in alarmArray) {
                    NSMutableDictionary *newItem = [NSMutableDictionary dictionaryWithDictionary:item];
                    NSNumber *time = [item objectForKey:@"time"];
                    if (time != nil) {
                        NSNumber *hour = [item objectForKey:@"timeHour"];
                        NSNumber *min = [item objectForKey:@"timeMin"];
                        [newItem setObject:hour forKey:@"hour"];
                        [newItem setObject:min forKey:@"minute"];
                    }
                    
                    [newAlarmArr addObject:newItem];
                }
                result(newAlarmArr);
            } else {
                result([NSArray array]);
            }
        }];
    }
    else if ([method isEqualToString:M_setNotificationConfig]) {
        NSNumber *notificationEnable = [params objectForKey:@"notificationEnable"];
        NSNumber *incomingEnable = [params objectForKey:@"incomingEnable"];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setNotificationConfig:[notificationEnable boolValue] incomingEnable:[incomingEnable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setFindMyPhone]) {
        number = [params objectForKey:KEY_VALUE];
        if (number != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setFindMyPhone: [number integerValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_sendFindPhoneSwitchStatus]) {
        number = [params objectForKey:KEY_ENABLE];
        if (number != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setIphoneFindSwitch:[number intValue] block:^(NSInteger errorCode) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setAutoStress]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil && [self isConnected]) {
            [[UTEDeviceMgr sharedInstance] setAutoStress:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_sendCameraPermission]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        enable = [params objectForKey:KEY_ENABLE];
        [[UTEDeviceMgr sharedInstance] setCameraSwitch:[enable intValue] block:^(NSInteger errorCode) {
            result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
        }];
    }
    else {
        consumed = NO;
    }
    
    return consumed;
}

@end
